package com.xx.servlet;


import com.xx.bean.User;
import com.xx.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    private UserServiceImpl userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User(null, username, password, null);
        try {
            // 登录成功
            userService.doLogin(user);
            resp.sendRedirect(req.getContextPath()+"/pages/user/login_success.html");
        } catch (Exception e) {
            resp.getWriter().write("登陆失败"+e.getMessage());
        }


    }
}
